Skip to content

fix(api): always use node:https.request in apiFetch to avoid undici b…#1319

Merged
Bret Comnes (bcomnes) merged 3 commits into
v1.xfrom
fix-inconsistant-timeouts
May 20, 2026
Merged

fix(api): always use node:https.request in apiFetch to avoid undici b…#1319
Bret Comnes (bcomnes) merged 3 commits into
v1.xfrom
fix-inconsistant-timeouts

Conversation

@bcomnes
Copy link
Copy Markdown
Member

@bcomnes Bret Comnes (bcomnes) commented May 19, 2026

…ody timeout

Node's built-in fetch uses undici which has a default bodyTimeout of 300s. For large streaming ND-JSON responses (e.g. full scan results) this timeout fires before the body has fully transferred, producing a BodyTimeoutError.

nodejs/undiciv6.21.3 —lib/dispatcher/client.js`, line ~246

this[kBodyTimeout] = bodyTimeout != null ? bodyTimeout : 300e3

Route all apiFetch calls through the existing _httpsRequestFetch path, which uses node:https.request and has no body timeout — consistent with the SDK and all other HTTP clients used across socket-cli, socket-sdk-js, and coana.

When SSL_CERT_FILE is not set, agent is passed as undefined and https.request uses its default agent. Behaviour is otherwise identical to before.

Update the apiFetch test to assert https.request is always used and that no custom HttpsAgent is created when CA certs are not configured.


Note

Medium Risk
All CLI API traffic now routes through the custom https.request implementation, which could subtly change request/redirect behavior compared to fetch and affect all outbound calls. Mitigated by reusing the existing _httpsRequestFetch path and tightening tests around agent handling.

Overview
apiFetch no longer falls back to global fetch; it now always uses the existing _httpsRequestFetch (node:https.request) path so large/streaming ND-JSON responses aren’t subject to undici’s default body timeout.

When no extra CA certs are configured, the request is made with agent: undefined (default agent); when SSL_CERT_FILE provides extra CAs, a custom HttpsAgent is still created and passed. Tests were updated to assert https.request is always used and that the agent behavior matches both scenarios.

Reviewed by Cursor Bugbot for commit ee86ba4. Configure here.

…ody timeout

Node's built-in fetch uses undici which has a default bodyTimeout of 300s.
For large streaming ND-JSON responses (e.g. full scan results) this timeout
fires before the body has fully transferred, producing a BodyTimeoutError.

Route all apiFetch calls through the existing _httpsRequestFetch path, which
uses node:https.request and has no body timeout — consistent with the SDK and
all other HTTP clients used across socket-cli, socket-sdk-js, and coana.

When SSL_CERT_FILE is not set, agent is passed as undefined and https.request
uses its default agent. Behaviour is otherwise identical to before.

Update the apiFetch test to assert https.request is always used and that no
custom HttpsAgent is created when CA certs are not configured.
Copilot AI review requested due to automatic review settings May 19, 2026 22:28
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR updates the CLI’s low-level API request helper to always use node:https.request instead of Node’s built-in fetch (undici), avoiding undici’s default 300s bodyTimeout that can interrupt long-running / large responses.

Changes:

  • Route all apiFetch calls through the existing https.request implementation path.
  • Allow _httpsRequestFetch to accept an HttpsAgent | undefined so the default agent is used when no extra CA certs are configured.
  • Update unit tests to assert https.request is always used and that no custom agent is created when CA certs are not configured.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

File Description
src/utils/api.mts Removes fetch fallback and always uses _httpsRequestFetch with an optional agent.
src/utils/api.test.mts Updates tests to reflect always-https.request behavior and validates agent handling when CA certs are absent.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread src/utils/api.mts
…ering

Resolving the promise as soon as headers arrive and constructing the
Response with a ReadableStream body, matching fetch() semantics.

Previously the response body was fully buffered before the promise
resolved, which broke streaming call sites such as streamDownloadWithFetch
that pipe response.body to disk expecting a live stream.
Comment thread src/utils/api.mts
@bcomnes Bret Comnes (bcomnes) merged commit 4c49d6e into v1.x May 20, 2026
12 checks passed
@bcomnes Bret Comnes (bcomnes) deleted the fix-inconsistant-timeouts branch May 20, 2026 00:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants